package cn.xiaoyuan.common;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MainInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

            //判断是login页面，是 直接放行 不是判断是否登录，登录放行，不登陆跳到登录界面
            String uri=request.getRequestURI();
            if(uri.endsWith("login")||uri.contains("login")){
                return true;
            }else {
                if (request.getSession().getAttribute("user") != null) {
                    return true;
                } else {
                    response.sendRedirect(request.getContextPath()+"/page/auth/login");
                }
            }
            //默认拦截
            return false;
    }
}
